package com.yiren.manager.ctrl.assets;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrRoomAssetComp;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.assets.YrRoomAssetCompForm;
import com.yiren.manager.service.iface.assets.YrRoomAssetCompService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("roomAssetComp")
public class YrRoomAssetCompController extends GenericController<YrRoomAssetCompForm, YrRoomAssetComp>{

	@Autowired
	private YrRoomAssetCompService selfService;
	
	@RequestMapping("save")
	@ResponseBody
	public HTTPResult<YrRoomAssetCompForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrRoomAssetCompForm> result = new HTTPResult<YrRoomAssetCompForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrRoomAssetCompForm form = tool.mapper("roomAssetComp", YrRoomAssetCompForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getRoomAssetCompId()||form.getRoomAssetCompId().isEmpty()){
			form.setRoomAssetCompId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrRoomAssetComp entity = ObjectUtils.objectToObject(form, YrRoomAssetComp.class);
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrRoomAssetCompForm> find(String beHospitalId,String elderId,String roomId) throws Exception{
		HTTPResult<YrRoomAssetCompForm> result = new HTTPResult<YrRoomAssetCompForm>();
		String sqlWhere = "room_id=?1 AND be_hospital_id=?2";
		QueryResult<YrRoomAssetComp> qr = selfService.find(YrRoomAssetComp.class, sqlWhere, new Object[]{roomId,beHospitalId});
		List<YrRoomAssetCompForm> formList = new ArrayList<YrRoomAssetCompForm>();
		for(YrRoomAssetComp entity : qr.getResultList()){
			YrRoomAssetCompForm form = toForm(entity);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("saveBatch")
	@ResponseBody
	public HTTPResult<YrRoomAssetCompForm> saveBatch(String data) throws MyException, SwallowException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrRoomAssetCompForm> result = new HTTPResult<YrRoomAssetCompForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		ArrayList<YrRoomAssetCompForm> formList = tool.mapper("roomAssetCompList", new TypeReference<ArrayList<YrRoomAssetCompForm>>() {});
		if(null==formList){
			throwExp("param.error");
		}
		for(YrRoomAssetCompForm form : formList){
			if(null==form.getRoomAssetCompId()||form.getRoomAssetCompId().isEmpty()){
				form.setRoomAssetCompId(UUIDGenerator.getUUID());
				form.setCreateTime(new Date());
			}
			form.setUpdateTime(new Date());
			YrRoomAssetComp entity = ObjectUtils.objectToObject(form, YrRoomAssetComp.class);
			selfService.save(entity);
		}
	
		return result;
	}
	
	@Override
	public YrRoomAssetCompForm toForm(YrRoomAssetComp e) throws Exception {
		YrRoomAssetCompForm form = ObjectUtils.objectToObject(e, YrRoomAssetCompForm.class);
		return form;
	}

	@Override
	public YrRoomAssetComp toEntity(YrRoomAssetCompForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
